import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.ProtocolException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.RedirectHandler;
import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.qq.spirit.HttpUtil;
import com.qq.spirit.image.OCR;

public class HttpClientTest
{

	public static void main(String[] args)
	{
		try
		{
			final DefaultHttpClient client = new DefaultHttpClient();

			client.setRedirectStrategy(new RedirectStrategy() {

				@Override
				public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException
				{
					int i = response.getStatusLine().getStatusCode();
					if ((i == 301) || (i == 302))
					{
						try
						{
							Header localHeader = response.getFirstHeader("location");
							if (localHeader != null)
							{
								HttpGet hg = new HttpGet(localHeader.getValue());
								client.execute(hg);
							}
						} catch (Exception localProtocolException)
						{
						}

						return true;
					}

					return false;
				}

				@Override
				public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException
				{
					// TODO Auto-generated method stub
					return null;
				}
			});

			List<BasicNameValuePair> paramList = new ArrayList<BasicNameValuePair>();
			paramList.add(new BasicNameValuePair("tourl", ""));
			paramList.add(new BasicNameValuePair("entergame", ""));
			paramList.add(new BasicNameValuePair("districtid", ""));
			paramList.add(new BasicNameValuePair("button", ""));
			paramList.add(new BasicNameValuePair("username", "cyb120"));
			paramList.add(new BasicNameValuePair("password", "123123"));
			HttpPost hp = new HttpPost("http://www.yaowan.com/?m=user&action=loginform");
			hp.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
			HttpResponse response = client.execute(hp);

			hp.abort();
			HttpGet hg = new HttpGet("http://www.yaowan.com/m=game&game_id=15&district_id=198");
			response = client.execute(hg);
			Iterator<Cookie> ite = client.getCookieStore().getCookies().iterator();
			while (ite.hasNext())
			{
				Cookie c = ite.next();
				System.out.println(c);
			}

		} catch (FailingHttpStatusCodeException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
